Executed: Mon Mar 27 22:24:18 2017

Duration: 12 seconds.


In [1]:
data_id = '17d'

In [2]:
ph_sel_name = "None"

In [3]:
data_id = "12d"

Multi-spot vs usALEX FRET histogram comparison

Load FRETBursts software


In [4]:
from fretbursts import *
sns = init_notebook()


 - Optimized (cython) burst search loaded.
 - Optimized (cython) photon counting loaded.
--------------------------------------------------------------
 You are running FRETBursts (version 0.5.9).

 If you use this software please cite the following paper:

   FRETBursts: An Open Source Toolkit for Analysis of Freely-Diffusing Single-Molecule FRET
   Ingargiola et al. (2016). http://dx.doi.org/10.1371/journal.pone.0160716 

--------------------------------------------------------------

In [5]:
import os
import pandas as pd
from IPython.display import display, Math

In [6]:
import lmfit
print('lmfit version:', lmfit.__version__)


lmfit version: 0.9.5

In [7]:
figure_size = (5, 4)
default_figure = lambda: plt.subplots(figsize=figure_size)
save_figures = True

def savefig(filename, **kwargs):
    if not save_figures:
        return
    import os
    dir_ = 'figures/'
    kwargs_ = dict(dpi=300, bbox_inches='tight')
                   #frameon=True, facecolor='white', transparent=False)
    kwargs_.update(kwargs)
    plt.savefig(dir_ + filename, **kwargs_)
    print('Saved: %s' % (dir_ + filename))

8-spot paper plot style


In [8]:
PLOT_DIR = './figure/'

In [9]:
import matplotlib as mpl
from cycler import cycler

bmap = sns.color_palette("Set1", 9)
colors = np.array(bmap)[(1,0,2,3,4,8,6,7), :]
mpl.rcParams['axes.prop_cycle'] = cycler('color', colors)
colors_labels = ['blue', 'red', 'green', 'violet', 'orange', 'gray', 'brown', 'pink', ]
for c, cl in zip(colors, colors_labels):
    locals()[cl] = tuple(c) # assign variables with color names
sns.palplot(colors)


Data files

Data folder:


In [10]:
data_dir = './data/multispot/'

Check that the folder exists:


In [11]:
data_dir = os.path.abspath(data_dir) + '/'
assert os.path.exists(data_dir), "Path '%s' does not exist." % data_dir

List of data files in data_dir:


In [12]:
from glob import glob
file_list = sorted(glob(data_dir + '*_?.hdf5'))

In [13]:
labels = ['12d', '7d', '17d', '22d', '27d', 'DO']
files_dict = {lab: fname for lab, fname in zip(sorted(labels), file_list)}
files_dict


Out[13]:
{'12d': '/Users/anto/Google Drive/notebooks/multispot_paper/data/multispot/12d_New_30p_320mW_steer_3.hdf5',
 '17d': '/Users/anto/Google Drive/notebooks/multispot_paper/data/multispot/17d_100p_320mW_steer_1.hdf5',
 '22d': '/Users/anto/Google Drive/notebooks/multispot_paper/data/multispot/22d_30p_320mW_steer_1.hdf5',
 '27d': '/Users/anto/Google Drive/notebooks/multispot_paper/data/multispot/27d_50p_320mW_steer_1.hdf5',
 '7d': '/Users/anto/Google Drive/notebooks/multispot_paper/data/multispot/7d_New_150p_320mW_steer_3.hdf5',
 'DO': '/Users/anto/Google Drive/notebooks/multispot_paper/data/multispot/DO12_No2_50p_320mW_steer_1.hdf5'}

Correction parameters

Multispot

Load the multispot leakage coefficient from disk (computed in Multi-spot 5-Samples analyis - Leakage coefficient fit):


In [14]:
_fname = 'results/Multi-spot - leakage coefficient KDE wmean DexDem.csv'
leakageM = np.loadtxt(_fname, ndmin=1)

print('Leakage coefficient:', leakageM)


Leakage coefficient: [ 0.0334]

Load the multispot direct excitation coefficient ($d_{dirT}$) from disk (computed in usALEX - Corrections - Direct excitation physical parameter):


In [15]:
_fname = 'results/usALEX - direct excitation coefficient dir_ex_t beta.csv'
dir_ex_tM = np.loadtxt(_fname, ndmin=1)

print('Direct excitation coefficient (dir_ex_t):', dir_ex_tM)


Direct excitation coefficient (dir_ex_t): [ 0.04932]

Load the multispot gamma ($\gamma_M$) coefficient (computed in Multi-spot Gamma Fitting):


In [16]:
_fname = 'results/Multi-spot - gamma factor.csv'
gammaM = np.loadtxt(_fname, ndmin=1)

print('Multispot gamma coefficient:', gammaM)


Multispot gamma coefficient: [ 0.45525]

usALEX

Load the usALEX leakage coefficient from disk (computed in usALEX - Corrections - Leakage fit):


In [17]:
_fname = 'results/usALEX - leakage coefficient DexDem.csv'
leakageA = np.loadtxt(_fname)

print('usALEX Leakage coefficient:', leakageA)


usALEX Leakage coefficient: 0.10029

Load the usALEX gamma coefficient (computed in usALEX - Corrections - Gamma factor fit):


In [18]:
_fname = 'results/usALEX - gamma factor - all-ph.csv'
gammaA = np.loadtxt(_fname)

print('usALEX Gamma-factor:', gammaA)


usALEX Gamma-factor: 1.020526

Load the usALEX beta coefficient (computed in usALEX - Corrections - Gamma factor fit):


In [19]:
_fname = 'results/usALEX - beta factor - all-ph.csv'
betaA = np.loadtxt(_fname)

print('usALEX Gamma-factor:', betaA)


usALEX Gamma-factor: 0.813553

Load the usALEX direct-excitation coefficient ($d_{exAA}$) (computed in usALEX - Corrections - Direct excitation fit):


In [20]:
_fname = 'results/usALEX - direct excitation coefficient dir_ex_aa.csv'
dir_ex_aa = np.loadtxt(_fname)

print('Direct excitation coefficient (dir_ex_aa):', dir_ex_aa)


Direct excitation coefficient (dir_ex_aa): 0.06062

Compute usALEX direct-excitation coefficient ($d_{exT}$) (see usALEX - Corrections - Direct excitation physical parameter):


In [21]:
dir_ex_tA = betaA * dir_ex_aa
dir_ex_tA


Out[21]:
0.04931758286

Parameters

Analysis parameters:


In [22]:
donor_ref = False    # False -> gamma correction is: g*nd + na
                     # True  -> gamma correction is: nd + na/g

hist_weights = 'size'

## Background fit parameters
bg_kwargs_auto = dict(fun=bg.exp_fit,
                 time_s = 30,
                 tail_min_us = 'auto',
                 F_bg=1.7,
                 )

## Burst search
F=6
dither = False
size_th = 30    # Burst size threshold (selection on corrected burst sizes)

## FRET fit parameters
bandwidth = 0.03        # KDE bandwidth
E_range = {'7d':  (0.7, 1.0), '12d': (0.4, 0.8), '17d': (0.2, 0.4), 
           '22d': (0.0, 0.1), '27d': (0.0, 0.1), 'DO': (0.0, 0.1)}
E_axis_kde = np.arange(-0.2, 1.2, 0.0002)

Utility functions


In [23]:
def print_fit_report(E_pr, gamma=1, leakage=0, dir_ex_t=0, math=True):
    """Print fit and standard deviation for both corrected and uncorrected E
    Returns d.E_fit.
    """
    E_corr = fretmath.correct_E_gamma_leak_dir(E_pr, gamma=gamma, leakage=leakage, dir_ex_t=dir_ex_t)
    
    E_pr_mean = E_pr.mean()*100
    E_pr_delta = (E_pr.max() - E_pr.min())*100
    
    E_corr_mean = E_corr.mean()*100
    E_corr_delta = (E_corr.max() - E_corr.min())*100
    if math:
        display(Math(r'\text{Pre}\;\gamma\quad\langle{E}_{fit}\rangle = %.1f\%% \qquad'
                     '\Delta E_{fit} = %.2f \%%' % \
                     (E_pr_mean, E_pr_delta)))
        display(Math(r'\text{Post}\;\gamma\quad\langle{E}_{fit}\rangle = %.1f\%% \qquad'
                     '\Delta E_{fit} = %.2f \%%' % \
                     (E_corr_mean, E_corr_delta)))
    else:
        print('Pre-gamma  E (delta, mean):  %.2f  %.2f' % (E_pr_mean, E_pr_delta))
        print('Post-gamma E (delta, mean):  %.2f  %.2f' % (E_corr_mean, E_corr_delta))

Multispot analysis


In [24]:
d = loader.photon_hdf5(files_dict[data_id])
d.calc_bg(**bg_kwargs_auto)
d.burst_search(m=10, F=F, dither=dither)


 - Calculating BG rates ... [DONE]
 - Performing burst search (verbose=False) ...[DONE]
 - Calculating burst periods ...[DONE]
 - Counting D and A ph and calculating FRET ... 
   - Applying background correction.
   - Applying leakage correction.
   [DONE Counting D/A]

In [25]:
d.time_max


Out[25]:
315.59361582499997

In [26]:
ds = Sel(d, select_bursts.size, th1=30, gamma=gammaM, donor_ref=donor_ref)

In [27]:
ds.num_bursts


Out[27]:
array([ 946,  592, 1135, 1500, 1375,  847, 1541, 1606])

In [28]:
# fitter = bext.bursts_fitter(ds)
# fitter.histogram(bins=np.r_[-0.2 : 1.2 : bandwidth])
# fitter.model = mfit.factory_two_gaussians(add_bridge=False, p2_center=0.4)
# fitter.fit_histogram()
# display(fitter.params['p2_center'])
# print_fit_report(fitter.params['p2_center'], gamma=gammaM, leakage=leakageM, dir_ex_t=dir_ex_tM)

In [29]:
dplot(ds, hist_fret);
      #show_model=True, show_fit_stats=True, fit_from='p2_center', show_fit_value=True);



In [30]:
d_all = ds.collapse()

In [31]:
d_all_chunk = Sel(d_all, select_bursts.time, time_s2=600/8)

In [32]:
dplot(d_all_chunk, hist_fret)


Out[32]:
<matplotlib.axes._subplots.AxesSubplot at 0x12bc70e10>

In [33]:
Eraw = d_all_chunk.E[0]

In [34]:
E = fretmath.correct_E_gamma_leak_dir(Eraw, gamma=gammaM, leakage=leakageM, dir_ex_t=dir_ex_tM)

In [35]:
sns.set_style('whitegrid')
%config InlineBackend.figure_format='retina'  # for hi-dpi displays

In [36]:
plt.hist(E, bins=np.arange(-0.2, 1.2, 0.025) + 0.5*0.025);


Comparison with usALEX


In [37]:
bursts_usalex = pd.read_csv('results/bursts_usALEX_{sample}_{ph_sel}_F{F:.1f}_m{m}_size{th}.csv'
                            .format(sample=data_id, ph_sel='Dex', m=10, th=30, F=7), index_col=0)
bursts_usalex


Out[37]:
E S bg_a bg_aa bg_d bp i_end i_start na naa nd nda nt size_raw t_end t_start width_ms
0 0.693422 0.648823 3.187 2.335 1.407 0 609 399 91.813 71.665 40.593 -0.146 204.070 211 0.145293 0.141990 3.3024
1 0.697261 0.768444 3.728 2.731 1.646 0 1394 1307 42.272 18.269 18.354 0.829 78.895 88 0.373403 0.369540 3.8625
2 0.082435 1.016076 2.119 1.553 0.936 0 1900 1862 2.881 -0.553 32.064 -0.097 34.392 39 0.540750 0.538554 2.1959
3 0.830743 0.455655 3.575 2.620 1.579 0 2823 2720 36.425 52.380 7.421 -0.164 96.227 104 0.833203 0.829499 3.7043
4 0.638726 0.727365 3.322 2.434 1.467 0 3765 3653 48.678 28.566 27.533 0.848 104.776 113 1.045994 1.042551 3.4425
... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ...
1687 0.707110 0.568654 1.783 1.167 0.797 9 1633866 1633808 22.217 23.833 9.203 -0.077 55.253 59 596.237641 596.235607 2.0348
1688 0.836003 0.608642 1.315 0.861 0.588 9 1634833 1634767 32.685 25.139 6.412 -0.057 64.236 67 596.616040 596.614538 1.5012
1689 0.771465 0.643750 2.719 1.780 1.216 9 1635977 1635865 53.281 38.220 15.784 -0.117 107.284 113 597.117997 597.114893 3.1039
1690 0.671056 0.528687 3.195 2.091 1.429 9 1636531 1636430 33.805 44.909 16.571 -0.138 95.284 102 597.242993 597.239346 3.6470
1691 0.697537 0.451002 1.960 1.283 0.877 9 1636650 1636580 21.040 36.717 9.123 -0.085 66.880 71 597.250240 597.248003 2.2374

1692 rows × 17 columns


In [38]:
Eraw_alex = bursts_usalex.E

In [39]:
E_alex = fretmath.correct_E_gamma_leak_dir(Eraw_alex, gamma=gammaA, leakage=leakageA, dir_ex_t=dir_ex_tA)

In [40]:
kws = dict(bins=np.arange(-0.2, 1.2, 0.025) + 0.5*0.025, histtype='step', lw=1.8)
plt.hist(E, label='Multispot', **kws)
plt.hist(E_alex, label='μs-ALEX', **kws)
plt.legend(loc=2)
plt.title('Sample %s: Multispot vs μs-ALEX comparison' % data_id)
plt.xlabel('FRET Efficiency')
plt.ylabel('# Bursts');
savefig('Multispot vs usALEX FRET hist comp sample %s' % data_id)


Saved: figures/Multispot vs usALEX FRET hist comp sample 12d

In [41]:
kws = dict(bins=np.arange(-0.2, 1.2, 0.025) + 0.5*0.025, histtype='step', lw=1.8, normed=True)
plt.hist(E, label='Multispot', **kws)
plt.hist(E_alex, label='μs-ALEX', **kws)
plt.legend(loc=2)
plt.title('Sample %s: Multispot vs μs-ALEX comparison' % data_id)
plt.xlabel('FRET Efficiency')
plt.ylabel('Probabiltity');
savefig('Multispot vs usALEX FRET hist comp sample %s normed' % data_id)


Saved: figures/Multispot vs usALEX FRET hist comp sample 12d normed

In [42]: